import CryptoJS from '../node_modules/crypto-js/index.js';
import HmacSha1 from '../node_modules/crypto-js/hmac-sha1.js';
import cryptoBase64 from '../node_modules/crypto-js/enc-base64.js';
// import config from '@/static/config.json'
import config from '../../package-B/static/config.json' with { type: 'json' }


// 加密
const encoded = (jsonData) => {
    return CryptoJS.enc.Utf8.parse(JSON.stringify(jsonData)).toString(CryptoJS.enc.Base64)
}

// 解密
const decrypt = (data) => {
    return JSON.parse(CryptoJS.enc.Base64.parse(data).toString(CryptoJS.enc.Utf8));
}

const getGlobalHeaders = (api, data) => {
    let apiKey = 'kami';//密钥标识
    let nonce = Math.round(89999999 * Math.random() + 10000000);
    let secret = '7525'//密钥值
    let timestamp = new Date().getTime();
    let message = "POST" + api + "?apiKey=" + apiKey + "&nonce=" + nonce + "&secret=" + secret + "&timestamp=" + timestamp;
    if (data) {
        message = "POST" + api + "?apiKey=" + apiKey + "&data=" + JSON.stringify(data) + "&nonce=" + nonce + "&secret=" + secret + "&timestamp=" + timestamp;
    }

    console.log('--messaeg--',message)

    let hash = cryptoBase64.stringify((HmacSha1(message, secret)));//签名
    let globalHeaders = {
        "X-Auth-Timestamp": timestamp,
        "X-Auth-Signature": hash,
        "X-Auth-Nonce": nonce,
        "X-Api-Key": apiKey,
        'X-Tenant-Id': config.APP_ID,
        'X-App-Id': config.APP_ID,
    }

    return globalHeaders
}

export const useCrypto = () => {
    return {
        encoded,
        decrypt,
        getGlobalHeaders
    }
}

